package org.jdemo.tools.gitignore;

import org.jdemo.util.IOUtil;

import java.io.File;

/**
 * CreateGitignore
 *
 * @author longlin(longlin@cyou-inc.com)
 * @date 2014/10/22
 * @since V1.0
 */
public class CreateGitignore {
    public static void main(String[] args) throws Exception {
        recursion("D:/myspace/space_demo/jdemo");
    }

    public static void recursion(String path) throws Exception{
        if (!new File(path).exists()) {
            return;
        }
        File[] files = new File(path).listFiles();
        for (File file : files) {
            if (file.getName().toLowerCase().equals(".git")) {
                continue;
            }
            if (file.isDirectory()) {
                if (file.listFiles().length == 0 &&
                        (file.getName().equalsIgnoreCase("java") || file.getName().equalsIgnoreCase("resources") || file.getName().equalsIgnoreCase("webapp"))) {
                    writeFile(file, "", false);
                } else {
//                    boolean isModule = file.list(new FilenameFilter() {
//                        @Override
//                        public boolean accept(File dir, String name) {
//                            return name.endsWith(".iml");
//                        }
//                    }).length > 0;
//                    if (isModule) {
//                        String content = "/" + file.getName() + ".iml\n/target\n";
//                        writeFile(new File("D:/myspace/space_demo/jdemo"), content, true);
//                    }
                    recursion(file.getAbsolutePath());
                }
            }
        }
    }

    public static void writeFile(File dir, String content, boolean append) {
        String gitignoreFilepath = dir.getAbsolutePath() + "/.gitignore";
        IOUtil.writeFile(content, gitignoreFilepath, "utf-8", append);
        System.out.println("create:" + gitignoreFilepath);
    }
}
